/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * 105, S.S Paradise Apartment, Munnireddy Layout, Horamavu, Banaswadi, Bangalore, India.
 */
package org.or5e.api.webcam.images;

import java.awt.Component;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.net.URL;

import org.or5e.api.webcam.util.ElapsedTimer;

public class FileGrabber implements ImageObserver {

    public FileGrabber() {
    }

    public boolean imageUpdate(Image im, int flags, int x, int y, int w, int h) {
        System.out.println("Called updateImage: " + im);
        return true;
    }

    static MediaTracker tracker = null;
    static int tracked = 0;

    public static PixelArray getPixels(String filename) {
        return getPixels(getImage(filename));
    }

    public static PixelArray getPixels(URL url) {
        return getPixels(getImage(url));
    }

    public static byte[][] getByteArray(URL url) {
        return getPixels(url).greyBytes();
    }

    public static byte[][] getByteArray(String filename) {
        return getPixels(filename).greyBytes();
    }

    public static Image getImage(String filename) {
        ElapsedTimer et = new ElapsedTimer();
        Image image = Toolkit.getDefaultToolkit().getImage(filename);
        System.out.println("Image read: " + et);
        return image;
    }

    public static Image getImage(String filename, Component c) {
        Image image = Toolkit.getDefaultToolkit().getImage(filename);
        MediaTracker mt = new MediaTracker(c);
        mt.addImage(image, 0);
        try {
            mt.waitForID(0);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        return image;
    }

    public static Image getImage(URL url) {
        Image image = Toolkit.getDefaultToolkit().getImage(url);
        return image;
    }

    public static Image getImage(URL url, Component c) {
        // this one does not return until the image has loaded
        Image image = getImage(url);
        MediaTracker mt = new MediaTracker(c);
        mt.addImage(image, 0);
        try {
            mt.waitForID(0);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        return image;
    }


    public static PixelArray getPixels(Image image) {

        PixelGrabber grabber
                = new PixelGrabber(image, 0, 0, -1, -1, true);
        try {
            if (grabber.grabPixels()) {
                int width = grabber.getWidth();
                int height = grabber.getHeight();
                int[] pixels = (int[]) grabber.getPixels();
                return new PixelArray(pixels, width, height);
            }
        } catch (Exception e) {
            System.out.println("Grab was interrupted");
        }
        return null;
    }
}

